// 存储相关 --------------------------------------------------------------------
// 设置
export const setStorage = (key, value, storageType = 'local') => {
  value = typeof value === 'object' ? JSON.stringify(value) : value
  if (storageType === 'session') {
    sessionStorage.setItem(key, value)
  } else {
    localStorage.setItem(key, value)
  }
}
// 获取
export const getStorage = (key, storageType = 'local') => {
  if (storageType === 'session') {
    return JSON.parse(sessionStorage.getItem(key))
  } else {
    return JSON.parse(localStorage.getItem(key))
  }
}
// 删除
export const removeStorage = (key, storageType = 'local') => {
  if (storageType === 'session') {
    localStorage.removeItem(key)
  } else {
    localStorage.removeItem(key)
  }
}

// 处理请求参数
export const handleParams = (params) => {
  const newParams = {}
  for (const key in params) {
    if (params[key]) newParams[key] = params[key]
  }
  return newParams
}

// 16进制颜色转为RGBA
export const HexToRGBA = (colorStr) => {
  // 将颜色字符串转换为小写
  colorStr = colorStr.toLowerCase()

  // 如果颜色字符串以'#'开头，则移除它
  if (colorStr.charAt(0) === '#') {
    colorStr = colorStr.substr(1)
  }

  // 如果颜色是16进制的6位格式，如'#rrggbb'，则转换为'rgba(rr, gg, bb, 1)'
  if (colorStr.length === 6) {
    let r = parseInt(colorStr.substr(0, 2), 16)
    let g = parseInt(colorStr.substr(2, 2), 16)
    let b = parseInt(colorStr.substr(4, 2), 16)
    return `rgba(${r}, ${g}, ${b}, 1)`
  }

  // 如果颜色是16进制的8位格式，如'#aarrggbb'，则转换为'rgba(rr, gg, bb, aa)'
  if (colorStr.length === 8) {
    let r = parseInt(colorStr.substr(2, 2), 16)
    let g = parseInt(colorStr.substr(4, 2), 16)
    let b = parseInt(colorStr.substr(6, 2), 16)
    let a = parseInt(colorStr.substr(0, 2), 16) / 255
    // return `rgba(${r}, ${g}, ${b}, ${a})`
    return `rgba(${r}, ${g}, ${b}, 1)`
  }

  // 对于不支持的格式，返回'rgba(0, 0, 0, 1)'
  return 'rgba(0, 0, 0, 1)'
}
// RGBA转为16进制颜色
export const rgbaToHex6 = (rgba) => {
  // 将RGBA颜色值转换为16进制字符串
  const regex = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d+|\d+\.\d+)\)$/
  const match = rgba.match(regex)
  if (match) {
    const [r, g, b] = match.slice(1, 4).map(Number)
    return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`
  }
  throw new Error('Invalid RGBA color format')
}

// 筛选一级路由标识
export const getFirstRoutePerms = (permission) => {
  const firstPermArr = permission.map((item) => {
    return item.split(':')[0]
  })
  return [...new Set(firstPermArr)]
}
// 筛选二级路由标识
export const getSecondRoutePerms = (permission) => {
  const secondPermArr = permission.map((item) => {
    const arr = item.split(':')
    return `${arr[0]}:${arr[1]}`
  })
  return [...new Set(secondPermArr)]
}
// 根据一级/二级路由标识筛选可以展示的路由
export const getRoutes = (firstRoutePerms, secondRoutePerms, asyncRoutes) => {
  // 判断是否是管理员
  if (firstRoutePerms[0] === '*') return asyncRoutes
  // 筛选一级
  const newArr = asyncRoutes.filter((item) => {
    return firstRoutePerms.includes(item.permission)
  })
  // 筛选二级
  return newArr.map((item) => {
    return {
      ...item,
      children: item.children.filter((item) => secondRoutePerms.includes(item.permission))
    }
  })
}
